package com.guiji.quartz.mapper;

import cn.hutool.core.date.DateTime;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.guiji.common.core.mybatisplus.cache.MybatisPlusRedisCache;
import com.guiji.common.core.mybatisplus.core.BaseMapperPlus;
import com.guiji.quartz.vo.CountVo;
import com.guiji.quartz.vo.SpiderNewsStat;
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-09-15 17:12
 **/
//@CacheNamespace(implementation = MybatisPlusRedisCache.class, eviction = MybatisPlusRedisCache.class)
@DS("slave")
public interface SpiderNewsStatMapper extends BaseMapperPlus<SpiderNewsStat> {
	@Select("SELECT COUNT(*) news_count,news_category FROM spider_news_stat WHERE create_time LIKE concat(#{t}, '%') AND stat_sign=1 AND news_error is NULL GROUP BY news_category")
	List<Map<String,String>> countPush(@Param("t") String t);

	@Select("SELECT COUNT(*) news_count,news_category FROM spider_news_stat WHERE create_time LIKE concat(#{t}, '%') AND stat_sign=1 AND news_error is NOT NULL GROUP BY news_category")
	List<Map<String,String>> countPushFail(@Param("t") String t);

//	@Select("select s.news_category,s.successSize download_success,p.successSize push_success,p.errorSize push_fail from\n" +
//		"(SELECT news_category,sum(news_count) successSize,sum(news_error) errorSize  from spider_news_stat WHERE stat_sign=0 and create_time LIKE concat(#{t}, '%') GROUP BY news_category) s\n" +
//		"left JOIN  \n" +
//		"(SELECT news_category,sum(news_count) successSize,sum(news_error) errorSize  from spider_news_stat WHERE stat_sign=1 and create_time LIKE concat(#{t}, '%') GROUP BY news_category) p\n" +
//		"on s.news_category=p.news_category\n" +
//		"UNION\n" +
//		"select p.news_category,s.successSize download_success,p.successSize push_success,p.errorSize push_fail from\n" +
//		"(SELECT news_category,sum(news_count) successSize,sum(news_error) errorSize  from spider_news_stat WHERE stat_sign=0 and create_time LIKE concat(#{t}, '%') GROUP BY news_category) s\n" +
//		"RIGHT JOIN  \n" +
//		"(SELECT news_category,sum(news_count) successSize,sum(news_error) errorSize  from spider_news_stat WHERE stat_sign=1 and create_time LIKE concat(#{t}, '%') GROUP BY news_category) p\n" +
//		"on s.news_category=p.news_category")
//	List<CountVo> countDetail(@Param("t") String t);

	@Select("select s.news_category,(s.successSize-(case when p.successSize is null then 0 else p.successSize end)) download_success,p.successSize push_success,p.errorSize push_fail,s504.error504 error504  from\n" +
		"(SELECT news_category,sum(news_count) successSize  from spider_news_stat WHERE create_time LIKE concat(#{t}, '%') GROUP BY news_category) s\n" +
		"left JOIN\n" +
		"(SELECT news_category,sum(news_error) error504  from spider_news_stat WHERE stat_sign=1 and news_error=1 and backup_1 like \"%\\\"code\\\":504%\" and create_time LIKE concat(#{t}, '%') GROUP BY news_category) s504\n" +
		"on  s.news_category=s504.news_category\n" +
		"left JOIN  \n" +
		"(SELECT news_category,sum(news_count) successSize,sum(news_error) errorSize  from spider_news_stat WHERE stat_sign=1 and create_time LIKE concat(#{t}, '%') GROUP BY news_category) p\n" +
		"on s.news_category=p.news_category")
	List<CountVo> countDetail(@Param("t") String t);
}
